package com.itcc.controller;

import com.itcc.entity.Address;
import com.itcc.entity.Cart;
import com.itcc.entity.Orders;
import com.itcc.entity.User;
import com.itcc.service.AddressService;
import com.itcc.service.CartService;
import com.itcc.service.OrdersService;
import com.itcc.service.impl.AddressServiceImpl;
import com.itcc.service.impl.CartServiceImpl;
import com.itcc.service.impl.OrdersServiceImpl;
import com.itcc.utils.Constants;
import com.itcc.utils.EmailUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.security.GeneralSecurityException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/order")
public class OrdersController extends BaseServlet {

    public String preView(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException {

        String uid = request.getParameter("uid");

        AddressService addressService = new AddressServiceImpl();
        List<Address> addressList = addressService.findAddressByUid(Integer.parseInt(uid));

        CartService cartService = new CartServiceImpl();
        List<Cart> cartList = cartService.findAll(Integer.parseInt(uid));

        request.setAttribute("addressList", addressList);
        request.setAttribute("cartList", cartList);

        return Constants.FORWARD + "/order.jsp";
    }

    public String create(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException {
        String uid = request.getParameter("uid");
        String sum = request.getParameter("sum");
        String aid = request.getParameter("aid");

        OrdersService ordersService = new OrdersServiceImpl();
        ordersService.createOrder(aid, uid, sum);

        return Constants.FORWARD + "order?method=show";
    }

    public String show(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException {

        //判断是否登录
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            request.setAttribute("msg", "请先登录！");
            return Constants.REDIRECT + "/login.jsp";
        }

        OrdersService ordersService = new OrdersServiceImpl();
        List<Orders> ordersList = ordersService.findOrdersByUid(user.getUid());

        request.setAttribute("ordersList", ordersList);

        return Constants.FORWARD + "/orderList.jsp";
    }

    public String detail(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException {

        String oid = request.getParameter("oid");

        OrdersService ordersService = new OrdersServiceImpl();
        Orders order = ordersService.findOrdersByOid(oid);

        request.setAttribute("order", order);

        return Constants.FORWARD + "/orderDetail.jsp";

    }

    public String success(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException, GeneralSecurityException {
        //获取参数
        HttpSession session1 = request.getSession();
        User user = (User) session1.getAttribute("loginUser");
        if (user == null) {
            // 用户未登录，可能需要重定向到登录页面
            return "redirect:/login";
        }
        String oid=request.getParameter("oid");

        boolean paymentSuccess = true;

        if (paymentSuccess) {
            // 更新订单状态为支付成功
            //支付成功，修改状态
            OrdersService ordersService=new OrdersServiceImpl();
            ordersService.updateStatusByOid(oid);

            // 发送邮件
            EmailUtils.sendEmail(user);

            return Constants.FORWARD+"order?method=show";

        }
        else{

            HttpSession session=request.getSession();
            session.setAttribute("msg","订单"+oid+"支付失败");
            return Constants.REDIRECT+"message.jsp";
        }
    }

    public String manage(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("loginUser");

        if (user == null || user.getUrole()==0) {
            request.setAttribute("msg", "您没有权限访问管理页面");
            return Constants.FORWARD + "/index.jsp"; // 重定向到错误页面
        }


        OrdersService ordersService = new OrdersServiceImpl();

        List<Orders> ordersList = ordersService.findAllOrders();

        request.setAttribute("ordersList",ordersList);

        return Constants.FORWARD + "/manageorders.jsp";

    }
}
